package algorithms.strings

/**
 * [Anagram](https://www.hackerrank.com/challenges/anagram)
 */
// Complete the anagram function below.
fun anagram(s: String): Int {
    if (s.length % 2 != 0) {
        return -1
    }
    val len = s.length / 2
    val bucket1 = Array(26) { 0 }
    val bucket2 = Array(26) { 0 }
    for (i in 0 until len) {
        bucket1[s[i] - 'a']++
    }
    for (i in len until s.length) {
        bucket2[s[i] - 'a']++
    }
    var ans = 0
    for (i in 0 until 26) {
        ans += kotlin.math.abs(bucket1[i] - bucket2[i])
    }
    return ans / 2
}